package m4;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;

public class Consumer4 {
    public static void main(String[] args) throws Exception{
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672); //默认端口可以不用设置
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection(); //连接
        Channel c = con.createChannel(); //通道
        //创建随机队列
        String queue = UUID.randomUUID().toString();
        c.queueDeclare(queue,false,true,true,null);
        //创建交换机
        c.exchangeDeclare("logs",BuiltinExchangeType.FANOUT);
        //绑定  第三个参数对fanout无效
        c.queueBind(queue,"logs","");

        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                String s1 = new String(delivery.getBody());
                System.out.println("收到的消息是： "+s1);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };
        //从队列接收消息
        c.basicConsume(queue,true,deliverCallback,cancelCallback);

    }
}
